package tum0r.server;

import com.alibaba.fastjson.JSON;
import tum0r.model.ProblemInformation;
import tum0r.model.SecurityInformation;
import tum0r.server.problem.ProblemBase;
import tum0r.webengine.annotation.Server;
import tum0r.webengine.config.ApplicationConfig;
import tum0r.webengine.model.server.ActionMapping;
import tum0r.webengine.utils.server.action.Action;

import java.util.ArrayList;
import java.util.LinkedList;

@Server(Mapping = "/security/info")
public class Information {
    public void about(Action<String> action) {
        LinkedList<SecurityInformation> result = new LinkedList<>();
        result.add(new SecurityInformation("创建者", "tum0r"));
        result.add(new SecurityInformation("描述", "用来作为信息安全练习的网站接口"));

        SecurityInformation aboutAPI = new SecurityInformation("接口说明", "所有的题目里面均包含的接口");
        SecurityInformation infoAPI = new SecurityInformation("info", "获取题目信息");
        infoAPI.Children.add(new SecurityInformation("ProblemTitle", "题目标题"));
        infoAPI.Children.add(new SecurityInformation("ProblemID", "题目ID，用来获取flag"));
        infoAPI.Children.add(new SecurityInformation("Creator", "出题人"));
        infoAPI.Children.add(new SecurityInformation("ProblemLevel", "题目等级"));
        infoAPI.Children.add(new SecurityInformation("CreateTime", "出题时间"));
        infoAPI.Children.add(new SecurityInformation("ProblemFunc", "题目接口"));
        infoAPI.Children.add(new SecurityInformation("ProblemParams", "题目接口的参数信息"));
        aboutAPI.Children.add(infoAPI);
        aboutAPI.Children.add(new SecurityInformation("description", "获取题目描述"));
        aboutAPI.Children.add(new SecurityInformation("tips", "获取题目提示"));
        aboutAPI.Children.add(new SecurityInformation("writeUp", "获取Write Up"));
        aboutAPI.Children.add(new SecurityInformation("writeUpFile", "获取Write Up文件"));
        result.add(aboutAPI);

        result.add(new SecurityInformation("获取所有题目", "/security/info/models"));

        SecurityInformation submitFlag = new SecurityInformation("提交flag", "/security/flag/submit");
        submitFlag.Children.add(new SecurityInformation("参数 problemID", "题目的ID"));
        submitFlag.Children.add(new SecurityInformation("参数 flag", "题目的flag"));
        result.add(submitFlag);

        action.callBack(JSON.toJSONString(result));
    }

    public void models(Action<String> action) {
        ArrayList<SecurityInformation> result = new ArrayList<>(ApplicationConfig.RESOURCE_MAPPINGS.size());
        for (ActionMapping mapping : ApplicationConfig.RESOURCE_MAPPINGS) {
            if (mapping.path.contains("problem")) {
                try {
                    Object temp = mapping.targetClass.getConstructor().newInstance();
                    if (temp instanceof ProblemBase) {
                        ProblemInformation information = ((ProblemBase) temp).getInformation();
                        result.add(new SecurityInformation(information.ProblemID, information.ProblemTitle, mapping.path));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        result.sort(new SecurityInformation());
        action.callBack(JSON.toJSONString(result));
    }
}
